/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-4-13
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import util.StringDealer;

import dao.entity.SegmentEntity;;

public class SegmentDao {
	public static boolean insertSegment(Connection conn, SegmentEntity segment){
		String sqlString = "insert into segment(api_id,page_id,segtype,segindex,begin_tag,end_tag,content,relevant) values(?,?,?,?,?,?,?,?)";
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sqlString);
			pstmt.setInt(1, segment.getApi_id());
			pstmt.setInt(2, segment.getPage_id());
			pstmt.setInt(3, segment.getType());
			pstmt.setInt(4, segment.getIndex());
			pstmt.setString(5, StringDealer.getStringForMaxLength(200, segment.getBegin_tag()));
			pstmt.setString(6, StringDealer.getStringForMaxLength(200, segment.getEnd_tag()));
			pstmt.setString(7, StringDealer.getStringForMaxLength(60000, segment.getContent()));
			pstmt.setString(8, StringDealer.getStringForMaxLength(60000,segment.getRelevant()));
			pstmt.executeUpdate();
			pstmt.close();
		} catch (SQLException e) {
//			e.printStackTrace();
			return false;
		}
		return true;
	}
	public static int insertSegment(Connection conn, ArrayList<SegmentEntity> segmentList){
		int okNum = 0;
		String sqlString = "insert into segment(api_id,page_id,segtype,segindex,begin_tag,end_tag,content,relevant) values(?,?,?,?,?,?,?,?)";
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sqlString);
			for (int i=0; i<segmentList.size(); ++i){
				pstmt.setInt(1, segmentList.get(i).getApi_id());
				pstmt.setInt(2, segmentList.get(i).getPage_id());
				pstmt.setInt(3, segmentList.get(i).getType());
				pstmt.setInt(4, segmentList.get(i).getIndex());
				pstmt.setString(5, StringDealer.getStringForMaxLength(200, segmentList.get(i).getBegin_tag()));
				pstmt.setString(6, StringDealer.getStringForMaxLength(200, segmentList.get(i).getEnd_tag()));
				pstmt.setString(7, StringDealer.getStringForMaxLength(60000, segmentList.get(i).getContent()));
				pstmt.setString(8, StringDealer.getStringForMaxLength(60000,segmentList.get(i).getRelevant()));
				try{
					pstmt.executeUpdate();
					++ okNum;
				} catch (SQLException e1){
//					e1.printStackTrace();
				}
			}
			pstmt.close();
		} catch (SQLException e) {
//			e.printStackTrace();
			return okNum;
		}
		return okNum;
	}
	public static ArrayList<SegmentEntity> getSegmentListByPageID(Connection conn, int page_id){
		ArrayList<SegmentEntity> result = new ArrayList<SegmentEntity>();
		String sqlString = "select id,api_id,segtype,segindex,begin_tag,end_tag,content,relevant from segment where page_id=?";
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sqlString);
			pstmt.setInt(1, page_id);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				SegmentEntity thisSeg = new SegmentEntity(rs.getInt(1), rs.getInt(2), page_id, rs.getInt(3), 
						rs.getInt(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8));
				result.add(thisSeg);
			}
			pstmt.close();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	public static ArrayList<SegmentEntity> getSegmentListByPageIDList(Connection conn, ArrayList<Integer> pageIDList, int partSize){
		ArrayList<SegmentEntity> result = new ArrayList<SegmentEntity>();
		int readyNum = 0;
		Statement stmt;
		try {
			stmt = conn.createStatement();
			while (readyNum < pageIDList.size()){
				int thisSize = partSize;
				if (pageIDList.size() - readyNum < thisSize){
					thisSize = pageIDList.size() - readyNum;
				}
				String sqlString = "select id,api_id,page_id,segtype,segindex,begin_tag,end_tag,content,relevant from segment where page_id=" 
					+ pageIDList.get(readyNum);
				for (int i=1; i<thisSize; ++i){
					sqlString = sqlString + " or page_id=" + pageIDList.get(readyNum + i);
				}
				ResultSet rs = stmt.executeQuery(sqlString);
				while(rs.next()){
					SegmentEntity thisSeg = new SegmentEntity(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getInt(4), 
							rs.getInt(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9));
					result.add(thisSeg);
				}
				rs.close();
				
				readyNum += partSize;
			}
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
